using System.IO;
using System.Text;
using NUnit.Framework;
using Should.Extensions.AssertExtensions;

namespace Cirrostrata.UnXaml.Core.Tests
{
    public class UnXamlParserContext
    {
        public string ParseTest(string input)
        {
            var text = new StringBuilder();

            var xamlGenerator = new XamlGeneratorVisitor(new StringWriter(text));

            var parser = new UnXamlParser(
                new Lexer(new StringReader(input)),
                xamlGenerator);

            parser.Parse();

            return text.ToString();
        }
    }

    [TestFixture]
    public class UnXamlParserTests : UnXamlParserContext
    {
        [Test]
        public void Parse_NothingIn_NothingOut()
        {
            ParseTest("")
                .ShouldBeEmpty();
        }

        [Test]
        public void Parse_SingleElementOnly_SingleSelfClosingXmlElement()
        {
            ParseTest("aa\n")
                .ShouldEqual("<aa />");
        }

        [Test]
        public void Parse_IndentedElement_IndentedElementIsChildOfPreviousElement()
        {
            ParseTest(
                "aa\n" + 
                "  bb\n"
                ).ShouldEqual(
                "<aa><bb /></aa>");
        }

        [Test]
        public void Parse_TwoIndentedElementa_IndentedElementsAreChildrenOfPreviousElement()
        {
            ParseTest(
                "aa\n" + 
                "  bb\n" + 
                "  cc\n"
                ).ShouldEqual(
                "<aa><bb /><cc /></aa>");
        }

        [Test]
        public void Parse_VaryingIndentLevels_ChildrenAreCorrectlyNested()
        {
            ParseTest(
                "aa\n" +
                "\tbb\n" +
                "\tcc\n" +
                "     dd\n" +
                "     ee\n" +
                "      ff\n" +
                "\tgg\n"
                ).ShouldEqual(
                    "<aa><bb /><cc><dd /><ee><ff /></ee></cc><gg /></aa>"
                );

            
        }
    }

}